﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LiJu
{
    public partial class ForceArmControl : UserControl
    {
        private float encoderSize = 30;
        /// <summary>
        /// 编码器形状大小
        /// </summary>
        [Description("编码器大小")]
        public float EncoderSize
        {
            get { return encoderSize; }
            set
            {
                encoderSize = value;
            }
        }

        public float ScrewSize = 20;

        private float rotateAngle1;
        /// <summary>
        /// 编码器1旋转角度
        /// </summary>
        [Description("旋转角度1")]
        public float RotateAngle1
        {
            get { return rotateAngle1; }
            set
            {
                rotateAngle1 = value;
                Invalidate();
            }
        }

        private float rotateAngle2;
        /// <summary>
        /// 编码器2旋转角度
        /// </summary>
        [Description("旋转角度2")]
        public float RotateAngle2
        {
            get { return rotateAngle2; }
            set
            {
                rotateAngle2 = value;
                Invalidate();
            }
        }

        private float rotateAngle3;
        /// <summary>
        /// 编码器3旋转角度
        /// </summary>
        [Description("旋转角度3")]
        public float RotateAngle3
        {
            get { return rotateAngle3; }
            set
            {
                rotateAngle3 = value;
                Invalidate();
            }
        }

        private float edgeLength1;
        [Description("臂长1")]
        [Category("自定义外观")]
        public float EgeLength1
        {
            get { return edgeLength1; }
            set
            {
                edgeLength1 = value;
            }
        }

        private float edgeLength2;
        [Description("臂长2")]
        [Category("自定义外观")]
        public float EgeLength2
        {
            get { return edgeLength2; }
            set
            {
                edgeLength2 = value;
            }
        }

        private float edgeLength3;
        [Description("臂长3")]
        [Category("自定义外观")]
        public float EgeLength3
        {
            get { return edgeLength3; }
            set
            {
                edgeLength3 = value;
            }
        }

        private bool positiveFlag;
        /// <summary>
        /// 电批正向标志
        /// </summary>
        [Description("正向标志")]
        public bool PositiveFlag
        {
            get { return positiveFlag; }
            set
            {
                positiveFlag = value;
                Invalidate();
            }
        }

        /// <summary>
        /// 编码器1颜色
        /// </summary>
        public Brush Encoder1Color = Brushes.Blue;
        /// <summary>
        /// 编码器2颜色
        /// </summary>
        public Brush Encoder2Color = Brushes.Orange;
        /// <summary>
        /// 编码器3颜色
        /// </summary>
        public Brush Encoder3Color = Brushes.Green;

        /// <summary>
        /// 电批正向颜色
        /// </summary>
        public Brush ScrewPositiveColor = Brushes.Black;

        /// <summary>
        /// 电批反向颜色
        /// </summary>
        public Brush ScrewNegativeColor = Brushes.Brown;

        /// <summary>
        /// 初始化
        /// </summary>
        public ForceArmControl()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.BackColor = Color.Transparent;

            EncoderSize = 40;
            RotateAngle1 = 0;
            RotateAngle2 = 0;
            RotateAngle3 = 0;

            EgeLength1 = 100;
            EgeLength2 = 100;
            EgeLength3 = 100;

            // 加载时启用默认双缓冲并提供无闪烁的图形呈现：
            this.DoubleBuffered = true;
            SetStyle(ControlStyles.UserPaint, true);
            // 禁止擦除背景.
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            // 双缓冲
            SetStyle(ControlStyles.DoubleBuffer, true);

            PositiveFlag = true;
        }


        /// <summary>
        /// 将初始矩形旋转指定的角度数
        /// </summary>
        /// <param name="g">窗体画图对象</param>
        /// <param name="rect">初始矩形对象(x,y,width,height)</param>
        /// <param name="rotateAngle">旋转角度，单位°</param>
        private void RotateRectangle(Graphics g, Rectangle rect, Brush brush, float rotateAngle)
        {
            // 保存原始状态
            GraphicsState state = g.Save();

            // 设置旋转的中心点为矩形的中心
            g.TranslateTransform(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);

            // 旋转矩形：旋转角度 从 X+ 到 Y+之间的旋转角度认为是正数
            g.RotateTransform(rotateAngle);

            // 恢复变换的中心点
            g.TranslateTransform(-(rect.X + rect.Width / 2), -(rect.Y + rect.Height / 2));

            // 绘制矩形
            g.FillRectangle(brush, rect);

            // 恢复图形状态
            g.Restore(state);
        }



        //编码器1起始坐标X
        private float baseX = 500;
        //编码器1起始坐标Y
        private float baseY = 20;
        /// <summary>
        /// 重绘图形轨迹
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            //// 绘制背景
            //g.Clear(Color.White);

            // 定义机械手臂的起点和终点
            PointF basePoint1 = new PointF(baseX, baseY);
            PointF endPoint1 = new PointF(baseX, baseY);
            // 计算手臂的终点位置
            double radians1 = rotateAngle1 * (Math.PI / 180);
            endPoint1.X = (float)(basePoint1.X - EgeLength1 * Math.Cos(radians1));
            endPoint1.Y = (float)(basePoint1.Y + EgeLength1 * Math.Sin(radians1));
            // 绘制手臂1
            g.DrawLine(new Pen(Encoder1Color, 3), basePoint1, endPoint1);
            // 绘制编码器1
            g.FillEllipse(Brushes.LightBlue, baseX - EncoderSize / 2, baseY - EncoderSize / 2, EncoderSize, EncoderSize);


            PointF basePoint2 = endPoint1;
            PointF endPoint2 = new PointF(baseX - EgeLength1, EgeLength1);
            // 计算手臂的终点位置
            double radians2 = rotateAngle2 * (Math.PI / 180);
            endPoint2.X = (float)(basePoint1.X - (EgeLength1 * Math.Cos(radians1) + EgeLength2 * Math.Cos(radians1 + radians2)));
            endPoint2.Y = (float)(basePoint1.Y + (EgeLength1 * Math.Sin(radians1) + EgeLength2 * Math.Sin(radians1 + radians2)));
            // 绘制手臂2
            g.DrawLine(new Pen(Encoder2Color, 3), basePoint2, endPoint2);
            // 绘制编码器2
            g.FillEllipse(Brushes.LightBlue, endPoint1.X - EncoderSize / 2, endPoint1.Y - EncoderSize / 2, EncoderSize, EncoderSize);

            PointF basePoint3 = endPoint2;
            PointF endPoint3 = new PointF(baseX - 2 * EgeLength1, EgeLength1);
            // 计算手臂的终点位置
            double radians3 = rotateAngle3 * (Math.PI / 180);
            endPoint3.X = (float)(basePoint1.X - (EgeLength1 * Math.Cos(radians1) + EgeLength2 * Math.Cos(radians1 + radians2) + EgeLength3 * Math.Cos(radians1 + radians2 + radians3)));
            endPoint3.Y = (float)(basePoint1.Y + (EgeLength1 * Math.Sin(radians1) + EgeLength2 * Math.Sin(radians1 + radians2) + EgeLength3 * Math.Sin(radians1 + radians2 + radians3)));
            // 绘制手臂3
            g.DrawLine(new Pen(Encoder3Color, 3), basePoint3, endPoint3);
            // 绘制编码器3
            g.FillEllipse(Brushes.LightBlue, endPoint2.X - EncoderSize / 2, endPoint2.Y - EncoderSize / 2, EncoderSize, EncoderSize);

            if (PositiveFlag)
            {
                // 绘制电批
                g.FillEllipse(ScrewPositiveColor, endPoint3.X - ScrewSize / 2, endPoint3.Y - ScrewSize / 2, ScrewSize, ScrewSize);
            }
            else
            {
                int recWidth = 40;
                int recHeight = 13;
                //double tanValue = (endPoint3.Y - basePoint1.Y) / (basePoint1.X - endPoint3.X);
                //double angle = Math.Tanh(tanValue) * (180 / Math.PI);
                // 设置矩形的位置和大小
                Rectangle rect = new Rectangle((int)(endPoint3.X - recWidth / 2), (int)(endPoint3.Y - recHeight / 2), recWidth, recHeight);
                RotateRectangle(g, rect, ScrewNegativeColor, 90 - rotateAngle1 - rotateAngle2 - rotateAngle3);
            }

        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            baseX = this.Width - EncoderSize/2-5;
            if (this.Width > this.Height)
            {
                EgeLength1 = EgeLength2 = EgeLength3 = (this.Height - baseY - EncoderSize / 2) / 3;
            }
            else
            {
                EgeLength1 = EgeLength2 = EgeLength3 = (this.Width - baseY - EncoderSize / 2) / 3;
            }
        }
    }

}
